﻿using System;
using System.Data;
using BLL;
using DAL;
using Ext.Net;
using Roles;

namespace ERP.Modules
{
    public partial class AssignedAccountLevel_1 : System.Web.UI.UserControl
    {
        #region[Define]
        private readonly BllDalAssignedAccountLevel1 _clsAccountLevel1 = new BllDalAssignedAccountLevel1();
        private readonly Income_Expenditure_Method_BLL _clsIncome = new Income_Expenditure_Method_BLL();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetAccountLevel1();
                GetIncome();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
                WindowsIncome_Expenditure_Method();
            }
        }
        //Store
        protected void StoreAssignedAccountLevel1_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            SelectBoxAssignedAccountLevel1Income.Text = string.Empty;
            GetAccountLevel1();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearText();
            WindowsAssignedAccountLevel1.Show();
            txtAccountLevel1_Name_Code.Focus(false, 100);
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grAssignedAccountLevel1.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    ClearText();
                    GetAccountLevel1Edit(int.Parse(row.RecordID));
                    WindowsAssignedAccountLevel1.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grAssignedAccountLevel1.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtAssignedAccountLevel1 { IDAccountLevel1 = int.Parse(row.RecordID) };
                        BllDalAssignedAccountLevel1.Delete(obj);
                        if (obj.Msg == 0)
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                    catch (Exception ex)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                    }
                }
            GetAccountLevel1();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsAssignedAccountLevel1.Close();
        }
        //Double-Click
        protected void AssignedAccountLevel1DblClick(object sender, DirectEventArgs e)
        {
            ClearText();
            int IDAccountLevel1 = int.Parse(e.ExtraParams["IDAccountLevel1"]);
            GetAccountLevel1Edit(IDAccountLevel1);
            WindowsAssignedAccountLevel1.Show();
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _dtSearch = _clsAccountLevel1.AssignedAccountLevel1Get(txtSearch.Text);
            if (_dtSearch != null && _dtSearch.Rows.Count > 0)
            {
                StoreAssignedAccountLevel1.DataSource = _dtSearch;
                StoreAssignedAccountLevel1.DataBind();
            }
            else
            {
                grAssignedAccountLevel1.GetStore().RemoveAll();
            }
        }
        //Select-Change
        protected void SelectBoxAssignedAccountLevel1Income_Change(object sender, DirectEventArgs e)
        {
            GetAccountLevel1();
            GetIncome();
        }
        //Store Reload
        protected void ReloadComboBox(object sender, StoreReadDataEventArgs e)
        {
            GetIncome();
        }
        #endregion

        #region[Method]
        private void GetAccountLevel1()
        {
            if (String.IsNullOrEmpty(SelectBoxAssignedAccountLevel1Income.Text))
            {
                var _dtGetAccountLevel1 = _clsAccountLevel1.AssignedAccountLevel1Get(txtSearch.Text);
                if (_dtGetAccountLevel1 != null && _dtGetAccountLevel1.Rows.Count > 0)
                {
                    StoreAssignedAccountLevel1.DataSource = _dtGetAccountLevel1;
                    StoreAssignedAccountLevel1.DataBind();
                }
                else
                {
                    grAssignedAccountLevel1.GetStore().RemoveAll();
                }
            }
            else
            {
                var _dtGetAccountLevel1 =
                    _clsAccountLevel1.AssignedAccountLevel1GetFillter(
                        int.Parse(SelectBoxAssignedAccountLevel1Income.SelectedItem.Value));
                if (_dtGetAccountLevel1 != null && _dtGetAccountLevel1.Rows.Count > 0)
                {
                    StoreAssignedAccountLevel1.DataSource = _dtGetAccountLevel1;
                    StoreAssignedAccountLevel1.DataBind();
                }
                else
                {
                    grAssignedAccountLevel1.GetStore().RemoveAll();
                }
            }
        }
        private void GetAccountLevel1Edit(int id)
        {
            var _dtGetAccountLevel1Edit = _clsAccountLevel1.AssignedAccountLevel1GetEdit(id);
            if (_dtGetAccountLevel1Edit != null && _dtGetAccountLevel1Edit.Rows.Count > 0)
            {
                txtAccountLevel1_ID.Text = _dtGetAccountLevel1Edit.Rows[0]["IDAccountLevel1"].ToString();
                txtAccountLevel1_Name_Code.Text = _dtGetAccountLevel1Edit.Rows[0]["AccountLevel1_Name_Code"].ToString();
                txtAccountLevel1_Name.Text = _dtGetAccountLevel1Edit.Rows[0]["AccountLevel1_Name"].ToString();
                cbIncomeMethod.SetValue(
                    int.Parse(_dtGetAccountLevel1Edit.Rows[0]["Income_Expenditure_Method_ID"].ToString()));
                txtNote.Text = _dtGetAccountLevel1Edit.Rows[0]["Note"].ToString();
            }
        }
        private void GetIncome()
        {
            var _dtIncome = _clsIncome.BindGirdtb_Income_Expenditure_Method(string.Empty);
            if (_dtIncome != null && _dtIncome.Rows.Count > 0)
            {
                StoreAssignedAccountLevel1Income.DataSource = _dtIncome;
                StoreAssignedAccountLevel1Income.DataBind();

                IncomeMethodStore.DataSource = _dtIncome;
                IncomeMethodStore.DataBind();
            }
            else
            {
                SelectBoxAssignedAccountLevel1Income.GetStore().RemoveAll();
                cbIncomeMethod.GetStore().RemoveAll();
            }
        }
        private bool CheckInsert()
        {
            bool kiemtra = false;
            var dtAcountLevel1 = _clsAccountLevel1.AssignedAccountLevel1Get(string.Empty);
            if (dtAcountLevel1 != null && dtAcountLevel1.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAcountLevel1.Rows)
                {
                    if (txtAccountLevel1_ID.Text != dr["IDAccountLevel1"].ToString())
                    {
                        if (txtAccountLevel1_Name_Code.Text == dr["AccountLevel1_Name_Code"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo",
                                "Mã tài khoản " + txtAccountLevel1_Name_Code.Text + " đã tồn tại !");
                        }
                        else if (txtAccountLevel1_Name.Text == dr["AccountLevel1_Name"].ToString() &&
                                 cbIncomeMethod.SelectedItem.Value == dr["Income_Expenditure_Method_ID"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo",
                                "Tên tài khoản " + txtAccountLevel1_Name.Text + " thuộc loại tài khoản " +
                                cbIncomeMethod.Text + " đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }
        private void Insert()
        {
            if (String.IsNullOrEmpty(txtAccountLevel1_ID.Text))
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtAssignedAccountLevel1
                    {
                        Income_Expenditure_Method_ID =
                            int.Parse(cbIncomeMethod.SelectedItem.Value),
                        AccountLevel1_Name_Code = txtAccountLevel1_Name_Code.Text,
                        AccountLevel1_Name = txtAccountLevel1_Name.Text,
                        Note = txtNote.Text
                    };
                    BllDalAssignedAccountLevel1.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới tài khoản thành công !");
                    GetAccountLevel1();
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtAssignedAccountLevel1
                    {
                        IDAccountLevel1 = int.Parse(txtAccountLevel1_ID.Text),
                        Income_Expenditure_Method_ID =
                            int.Parse(cbIncomeMethod.SelectedItem.Value),
                        AccountLevel1_Name_Code = txtAccountLevel1_Name_Code.Text,
                        AccountLevel1_Name = txtAccountLevel1_Name.Text,
                        Note = txtNote.Text
                    };
                    BllDalAssignedAccountLevel1.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật tài khoản thành công !");
                    WindowsAssignedAccountLevel1.Close();
                    GetAccountLevel1();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[Clear-Text]
        private void ClearText()
        {
            txtAccountLevel1_ID.Text = string.Empty;
            txtAccountLevel1_Name_Code.Text = string.Empty;
            txtAccountLevel1_Name.Text = string.Empty;
            txtNote.Text = string.Empty;
            cbIncomeMethod.Text = string.Empty;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.AssignedAccountLeve, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelAssignedAccountLevel1.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.AssignedAccountLeve, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion

        private void WindowsIncome_Expenditure_Method()
        {
            Window win = new Window()
            {
                ID = "WindowsIncome_Expenditure_Method",
                Title = "Loại tài khoản",
                Width = 829,
                Height = 400,
                Modal = true,
                AutoRender = false,
                Collapsible = false,
                Maximizable = true,
                Hidden = true,
                Loader = new ComponentLoader
                {
                    Url = "../PageParrent/Income_Expenditure_Method.aspx",
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true
                    }
                }
            };
            this.Controls.Add(win);
        }
    }
}